package org.jeecg.modules.inspect.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.inspect.entity.InsClassesUnusualMsg;
import org.jeecg.modules.inspect.entity.InsClassesUnusualMsgCourse;
import org.jeecg.modules.inspect.mapper.InsClassesUnusualMsgCourseMapper;
import org.jeecg.modules.inspect.mapper.InsClassesUnusualMsgIssueMapper;
import org.jeecg.modules.inspect.mapper.InsClassesUnusualMsgMapper;
import org.jeecg.modules.inspect.service.IInsClassesUnusualMsgCourseService;
import org.jeecg.modules.inspect.service.IInsClassesUnusualMsgService;
import org.jeecg.modules.inspect.util.SmsUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 异常信息
 * @Author: jeecg-boot
 * @Date:   2024-03-16
 * @Version: V1.0
 */
@Service
public class InsClassesUnusualMsgServiceImpl extends ServiceImpl<InsClassesUnusualMsgMapper, InsClassesUnusualMsg> implements IInsClassesUnusualMsgService {

	@Autowired
	private InsClassesUnusualMsgMapper insClassesUnusualMsgMapper;
	@Autowired
	private InsClassesUnusualMsgIssueMapper insClassesUnusualMsgIssueMapper;
	@Autowired
	private InsClassesUnusualMsgCourseMapper insClassesUnusualMsgCourseMapper;

	@Autowired
	private IInsClassesUnusualMsgCourseService insClassesUnusualMsgCourseService;
	@Autowired
	private ISysUserService sysUserService;
	@Resource
	private ISysBaseAPI sysBaseApi;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		insClassesUnusualMsgIssueMapper.deleteByMainId(id);
		insClassesUnusualMsgCourseMapper.deleteByMainId(id);
		insClassesUnusualMsgMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			insClassesUnusualMsgIssueMapper.deleteByMainId(id.toString());
			insClassesUnusualMsgCourseMapper.deleteByMainId(id.toString());
			insClassesUnusualMsgMapper.deleteById(id);
		}
	}

	@Override
	public void updateUnusualStatusById(String unusalId) {
		if(StringUtils.isBlank(unusalId)){
			return;
		}
		InsClassesUnusualMsg unusalMsg = this.getById(unusalId);
		List<InsClassesUnusualMsgCourse> courseList = insClassesUnusualMsgCourseService.list(Wrappers.<InsClassesUnusualMsgCourse>lambdaQuery().eq(InsClassesUnusualMsgCourse::getUnusualId, unusalId));

		if(CollectionUtils.isNotEmpty(courseList)){
			boolean isAllResolved=false;//已全部解决
			//过滤出等于未解决的数据
			List<InsClassesUnusualMsgCourse> solveList = courseList.stream().filter(o -> "1".equals(o.getStatus())).collect(Collectors.toList());
			if(CollectionUtils.isNotEmpty(solveList)){//不为空证明存在未解决的数据
				//修改主表状态为未解决
				unusalMsg.setStatus("1");
			}else{
				unusalMsg.setStatus("2");
			}
			this.updateById(unusalMsg);
		}
	}

	public void sendSMS(List<String> userIds) {
		// ==================== 发送短信开始 ===============================
		String phones = "";

		List<SysUser> sysUsers = sysUserService.listByIds(userIds);
		List<String> userNames = sysUsers.stream().map(SysUser::getUsername).collect(Collectors.toList());

		if(CollUtil.isNotEmpty(userNames)) {
			List<String> sendPhones = new ArrayList<>();
			for(String userName : userNames) {
				LoginUser loginUser = sysBaseApi.getUserByName(userName);
				sendPhones.add(loginUser.getPhone());
			}
			if(CollUtil.isNotEmpty(sendPhones)) {
				phones = String.join(",", sendPhones.stream().map(String::valueOf).distinct().collect(Collectors.toList()));
			}
		}

		if(StrUtil.isNotEmpty(phones)) {
			log.debug("发送短信提醒厂领导：" + phones);
			// 发送厂领导任务生成短信提醒
			String smsContent = "您有新的异常问题待处理，请注意查收！";
			SmsUtil.sendMas(phones.split(","), smsContent);
		}
		// 发送短信结束
	}

}
